<template>
    <div class="holiday wrap">
        <div class="tit">
            <p>Note：Classes are not scheduled during holidays.</p>
            <button class="btn" @click="isShowDialog=true">+Add</button>
            <div class="clear"></div>
        </div>
        
        <el-table :data="tableData" empty-text="No data available for the time being" border v-loading="loading">
            <el-table-column prop="f_holiday_name" label="Holiday Name"></el-table-column>
            <el-table-column prop="starttimestamp" :formatter="dateFormat" label="Start Date"></el-table-column>
            <el-table-column prop="endtimestamp" :formatter="dateFormat" label="End Date"></el-table-column>
            <el-table-column label="Operation" width="148">
                <template slot-scope="scope">
                    <el-button @click="getHolidayInfo(scope.row.f_id)">Edit</el-button>
                    <el-button type="danger" @click="delHoliday(scope.row.f_id)">Delete</el-button>
                </template>
            </el-table-column>
        </el-table>

        <el-pagination background @current-change="getPage" :total="pageCount"></el-pagination>

        <el-dialog custom-class="dialog" @closed="dialogOnClose" :title="form.id ? 'Edit' : 'Add'" :visible.sync="isShowDialog" :close-on-click-modal="false" :close-on-press-escape="false" width="480px">
            <el-form :model="form" ref="form" :rules="rules" label-width="120px" label-position="right">
                <el-form-item label="Holiday Name" prop="holidayName">
                    <el-input v-model.trim="form.holidayName" placeholder="Please Enter Holiday Name"></el-input>
                </el-form-item>
                <el-form-item label="Date Range" prop="dateRange">
                    <el-date-picker v-model="form.dateRange" value-format="timestamp" type="daterange" range-separator="-" start-placeholder="Start Date" end-placeholder="End Date"></el-date-picker>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="isShowDialog=false">Cancel</el-button>
                <el-button :loading="btnLoading" type="primary" @click="editHoliday" v-if="form.id">OK</el-button>
                <el-button :loading="btnLoading" type="primary" @click="addHoliday" v-else>OK</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        name: 'HolidaySet',
        data() {
            return {
                loading: false,
                btnLoading: false,
                tableData: [],
                pageNumber: 1,
                pageCount: 0,
                isShowDialog: false,
                form: {
                    id: '',
                    holidayName: '',
                    dateRange: []
                },
                rules: {
                    holidayName: [
                        {required: true, message: 'Holiday name cannot be empty!', trigger: 'blur'}
                    ],
                    dateRange: [
                        {required: true, message: 'Time range cannot be empty!', trigger: 'blur'}
                    ]
                }
            }
        },
        created() {
            this.getData()
        },
        methods: {
            // 初始化数据
            async getData() {
                this.loading = true

                const res = await this.axios.get(`/OnlineTeacher/ForeignFestival/GetListForeignFestival?pageNum=${this.pageNumber}&pageSize=10`)

                this.loading = false
                this.tableData = res.data
                this.pageCount = res.rowcount

                // 如果删除后当前页为空返回返回上一页
                if (!this.tableData.length && this.pageCount) {
                    this.pageNumber--
                    this.getData()
                }
            },
            // 格式化表格时间
            dateFormat(row, col) {
                const date = row[col.property]

                if (date === undefined) return ''

                return this.moment(date).format('YYYY-MM-DD')
            },
            // 获取节假日详情
            async getHolidayInfo(id) {
                const res = await this.axios.get(`/OnlineTeacher/ForeignFestival/GetDetailForeignFestival?id=${id}`)

                if (res.code === 1) {
                    this.isShowDialog = true

                    this.$nextTick(() => {
                        this.$set(this.form, 'id', res.data.f_id)
                        this.$set(this.form, 'holidayName', res.data.f_holiday_name)
                        this.$set(this.form, 'dateRange', [res.data.starttimestamp, res.data.endtimestamp])
                    })
                }
            },
            // 获取分页数据
            getPage(num) {
                this.pageNumber = num
                this.getData()
            },
            // 监听dialog关闭事件
            dialogOnClose() {
                this.$refs.form.resetFields()
                this.form.id = ''
            },
            // 添加节假日
            addHoliday() {
                this.$refs.form.validate(async valid => {
                    if (!valid) return

                    this.btnLoading = true

                    const data = this.qs.stringify({
                        f_holiday_name: this.form.holidayName,
                        starttimestamp: this.form.dateRange[0],
                        endtimestamp: this.form.dateRange[1]
                    })

                    const res = await this.axios.post('/OnlineTeacher/ForeignFestival/AddForeignFestival', data)

                    this.btnLoading = false

                    if (res.code === 1) {
                        this.isShowDialog = false
                        this.$message.success('Added Successful!')

                        this.getData()
                    }
                })
            },
            // 编辑节假日
            editHoliday() {
                this.$refs.form.validate(async valid => {
                    if (!valid) return

                    this.btnLoading = true

                    const data = this.qs.stringify({
                        f_id: this.form.id,
                        f_holiday_name: this.form.holidayName,
                        starttimestamp: this.form.dateRange[0],
                        endtimestamp: this.form.dateRange[1]
                    })

                    const res = await this.axios.post('/OnlineTeacher/ForeignFestival/EditForeignFestival', data)

                    this.btnLoading = false
                    
                    if (res.code === 1) {
                        this.isShowDialog = false
                        this.$message.success('Editorial Successful!')

                        this.getData()
                    }
                })
            },
            // 删除节假日
            delHoliday(id) {
                this.$confirm('Confirm to delete holidays?', 'Tips').then(async () => {
                    const res = await this.axios.get(`/OnlineTeacher/ForeignFestival/DeleteForeignFestival?id=${id}`)

                    if (res.code === 1) {
                        this.$message.success('Successful deletion!')
                        this.getData()
                    } else {
                        this.$message.success('Delete failed!')
                    }
                }).catch(() => {})
            }
        }
    }
</script>

<style>
    .holiday .dialog .el-dialog__header {border-bottom: 1px solid #DCE2EE; margin-bottom: 10px;}
    .holiday .dialog .el-dialog__footer {border-top: 1px solid #DCE2EE; padding: 20px 20px 20px;}
    .holiday .dialog .el-dialog__body {padding: 30px 20px 20px;}
    .holiday .dialog .el-date-editor {width: 100%;}
    .holiday .el-table .el-button {padding: 0; width: 56px; height: 28px; font-weight: normal;}
    .holiday .el-pagination {margin-top: 20px; text-align: center;}
    .holiday .is-leaf {color: #000; font-size: 18px; background: #D9E6FF;}
</style>

<style scoped>
    .wrap {width: 1000px; min-height: 462px; border-radius: 4px; background: #fff; float: right; padding: 22px;}
    .tit {margin-bottom: 10px;}
    .tit p {color: #9B9B9B; font-size: 18px; line-height: 36px; float: left;}
    .btn {width: 74px; height: 36px; background: #4D84FC; font-size: 16px; color: #fff; border-radius: 4px; box-shadow: 0 0 10px rgba(77, 132, 252, .8); float: right;}
</style>